package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.pojo.dto.VmTypeDto;
import com.heima.pojo.dto.VmTypeSearchDto;
import com.heima.pojo.entity.VmType;
import com.heima.mapper.VmTypeMapper;
import com.heima.pojo.vo.VmTypeSearchVo;
import com.heima.service.VmTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 售货机类型表 服务实现类
 * </p>
 *
 * @author ljk
 * @since 2024-10-20
 */
@Service
@RequiredArgsConstructor
public class VmTypeServiceImpl extends ServiceImpl<VmTypeMapper, VmType> implements VmTypeService {
    private final VmTypeMapper vmTypeMapper;

    @Override
    public VmTypeSearchVo pageQuery(VmTypeSearchDto vmTypeSearchDto) {
        IPage<VmType> page = new Page<>(vmTypeSearchDto.getPageIndex(),vmTypeSearchDto.getPageSize());
        LambdaQueryWrapper<VmType> queryWrapper = new LambdaQueryWrapper<>();
        boolean isEmpty = vmTypeSearchDto.getName() != null;
        queryWrapper.like(isEmpty,VmType::getName,vmTypeSearchDto.getName());
        IPage<VmType> list = vmTypeMapper.selectPage(page,queryWrapper);

        return VmTypeSearchVo.builder()
                .pageIndex(String.valueOf(list.getCurrent()))
                .pageSize(String.valueOf(list.getSize()))
                .totalCount(String.valueOf(list.getTotal()))
                .totalPage(String.valueOf(list.getPages()))
                .currentPageRecords(list.getRecords())
                .build();
    }

    @Override
    public void insertVmType(VmTypeDto vmTypeDto) {
        VmType vmType = new VmType();
        BeanUtils.copyProperties(vmTypeDto,vmType);
        vmTypeMapper.insert(vmType);
    }

    @Override
    public void updateVmType(Integer typeId, VmTypeDto vmTypeDto) {
        VmType vmType = new VmType();
        BeanUtils.copyProperties(vmTypeDto,vmType);
        vmType.setTypeId(typeId);
        vmTypeMapper.updateById(vmType);
    }

    @Override
    public void delVmType(Integer typeId) {
        vmTypeMapper.deleteById(typeId);
    }
}
